package com.ljfiction.book.activitys;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.ljfiction.book.Baner.CreadChapterBookBean;
import com.ljfiction.book.Baner.SketchBean;
import com.ljfiction.book.Dialog.NoBackgLodDialog;
import com.ljfiction.book.Dialog.PayPursDialog;
import com.ljfiction.book.Dialog.ThreeDialog;
import com.ljfiction.book.MyView.DividerItemDecoration;
import com.ljfiction.book.R;
import com.ljfiction.book.adapter.SketchAdapter;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.tools.SettingManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.request.BaseRequest;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class SketchActivity extends BaseActivity {

    @BindView(R.id.back_sketch)
    ImageView backSketch;
    @BindView(R.id.sketch_recycle)
    RecyclerView sketchRecycle;
    @BindView(R.id.nodatas_sketch)
    LinearLayout nodatasSketch;
    @BindView(R.id.refresh_sketch)
    SmartRefreshLayout refreshSketch;

    private SketchAdapter mAdapter;
    private List<SketchBean.DataBean.ResultListBean> list = new ArrayList<>();
    private ThreeDialog isOrNoDislog;
    private Context context;
    private String bookId;
    private SketchBean sketchBean;
    private CreadChapterBookBean creadChapterBookBean;
    private Intent intent;//去编辑界面
    private Intent intent1;//去发布界面
    private int bookIsVip = 0;
    private NoBackgLodDialog noBackgLodDialog;
    private boolean isfirst=true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sketch);
        ButterKnife.bind(this);
        context = this;
        bookId = getIntent().getStringExtra("bookId");
        bookIsVip = getIntent().getIntExtra("isVip", 0);
        intent = new Intent(this, ArticleActivity.class);
        intent.putExtra("isvip", bookIsVip);
        intent1 = new Intent(this, CreadChapterOkActivity.class);
        intView();
        intData();
    }

    private void intData() {
        OkHttpUtils.post(HttpManager.IP + "/chapter-getdrafts")

                .params("bookid", bookId)
                .execute(new StringCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        if (isfirst){
                            if (noBackgLodDialog == null) {
                                noBackgLodDialog = new NoBackgLodDialog(SketchActivity.this, true);
                            }
                            noBackgLodDialog.show();
                            isfirst=false;
                        }

                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (noBackgLodDialog != null) {
                            noBackgLodDialog.dismis();
                        }
                        refreshSketch.finishRefresh();
                        Gson gs = new Gson();
                        try {
                            sketchBean = gs.fromJson(s, SketchBean.class);
                            if (sketchBean.getReturnCode() == 200) {
                                list.clear();

                                for (SketchBean.DataBean i :
                                        sketchBean.getData()) {
                                    list.addAll(i.getResultList());
                                }
                                if (list.size() < 1) {
                                    nodatasSketch.setVisibility(View.VISIBLE);
                                }else{
                                    nodatasSketch.setVisibility(View.GONE);
                                }
                                mAdapter.notifyDataSetChanged();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void intView() {
        mAdapter = new SketchAdapter(R.layout.sketch_item, list);
        sketchRecycle.setAdapter(mAdapter);
        sketchRecycle.setLayoutManager(new LinearLayoutManager(this));
        sketchRecycle.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            intent.putExtra("isCaogao", 1);
            intent.putExtra("bookData", list.get(position));
            startActivity(intent);
        });
        mAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(final BaseQuickAdapter adapter, View view, final int position) {
                isOrNoDislog = new ThreeDialog(context, true);
                isOrNoDislog.setListener(new ThreeDialog.Listener() {
                    @Override
                    public void isOkClick() {
                        creadChapterBookBean = new CreadChapterBookBean();
                        creadChapterBookBean.setId(String.valueOf(list.get(position).getId()));
                        creadChapterBookBean.setBookisvip(bookIsVip);
                        creadChapterBookBean.setBookId(bookId);
                        creadChapterBookBean.setVolumeId(String.valueOf(list.get(position).getVolumeId()));
                        creadChapterBookBean.setBookTitle(list.get(position).getBookTitle());
                        creadChapterBookBean.setChapterTitle(list.get(position).getChapterTitle());
                        creadChapterBookBean.setChapterContent(list.get(position).getChapterContent());
                        creadChapterBookBean.setAuthorWords(list.get(position).getAuthorWords());
                        creadChapterBookBean.setReleastime(0);
                        creadChapterBookBean.setWhereCome(0);
                        creadChapterBookBean.setIsCaogao(1);
                        intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent1.putExtra("bookBean", creadChapterBookBean);
                        startActivity(intent1);
                    }

                    @Override
                    public void okClick() {
                        OkHttpUtils.post(HttpManager.IP + "/chapter-deletedrafts")

                                .params("chapterid", String.valueOf(list.get(position).getId()))
                                .params("bookid", bookId)
                                .execute(new StringCallback() {
                                    @Override
                                    public void onBefore(BaseRequest request) {
                                        super.onBefore(request);
                                        if (noBackgLodDialog == null) {
                                            noBackgLodDialog = new NoBackgLodDialog(SketchActivity.this, true);
                                        }
                                        noBackgLodDialog.show();
                                    }

                                    @Override
                                    public void onSuccess(String s, Call call, Response response) {
                                        if (noBackgLodDialog != null) {
                                            noBackgLodDialog.dismis();
                                        }
                                        try {
                                            JSONObject js = new JSONObject(s);
                                            if (js.getInt("returnCode") == 200) {
                                                list.remove(position);
                                                adapter.notifyDataSetChanged();
                                            }
                                            showToast(js.getString("mag"));
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });


                    }

                    @Override
                    public void closeClick() {
                    }
                }, "发布", "取消", "删除", true);
                return false;
            }
        });
        refreshSketch.setOnRefreshListener(refreshlayout -> intData());
    }

    @OnClick(R.id.back_sketch)
    public void onViewClicked() {
        goBack();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        intData();
    }
}
